//
//  JTLocationPicker.m
//  JiongTu
//
//  Created by 杨璟 on 16/4/12.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "JTLocationPicker.h"

@interface JTLocationPicker ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSDictionary *cityDict;

@property (nonatomic, strong) NSArray *cityArray;
@property (nonatomic, strong) NSArray *subLocalityArray;

@end

@implementation JTLocationPicker

/**
 *  单例对象的全局变量
 */
static JTLocationPicker *_instance;

/**
 *  返回单例对象
 */
+ (instancetype)sharedPicker {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
        _instance.delegate = _instance;
        _instance.dataSource = _instance;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"location" ofType:@"plist"];
        _instance.cityDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    });
    return _instance;
}

/**
 *  重写单例对象的alloc方法
 */
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

/**
 *  重写单例对象的copy方法
 */
+ (instancetype)copyWithZone:(struct _NSZone *)zone {
    return _instance;
}

- (void)setCityDict:(NSDictionary *)cityDict {
    _cityDict = cityDict;
    
    self.cityArray = [cityDict allKeys];
}

- (void)setCityArray:(NSArray *)cityArray {
    _cityArray = cityArray;
    
    NSInteger selectedCityIndex = [_instance selectedRowInComponent:0];
    NSString *seletedCity = [cityArray objectAtIndex:selectedCityIndex];
    self.subLocalityArray = [self.cityDict objectForKey:seletedCity];
}

#pragma mark - 代理

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return self.cityArray.count;
            break;
        case 1:
            return self.subLocalityArray.count;
            break;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return self.cityArray[row];
            break;
        case 1:
            return self.subLocalityArray[row];
            break;
    }
    return 0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        NSString *seletedCity = self.cityArray[row];
        self.subLocalityArray = [self.cityDict objectForKey:seletedCity];
        [_instance reloadComponent:1];
        
        NSInteger selectedCityIndex = [_instance selectedRowInComponent:1];
        NSString *seletedSublocality = self.subLocalityArray[selectedCityIndex];
        
        NSString *msg = [NSString stringWithFormat:@"%@%@", seletedCity,seletedSublocality];
        _instance.cityAndSubloca = msg;
    } else {
        NSInteger selectedCityIndex = [_instance selectedRowInComponent:0];
        NSString *seletedCity = self.cityArray[selectedCityIndex];
        
        NSString *seletedSublocality = self.subLocalityArray[row];
        
        NSString *msg = [NSString stringWithFormat:@"%@%@", seletedCity,seletedSublocality];
        _instance.cityAndSubloca = msg;
    }
    
}

@end
